﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Renci.SshNet;

namespace XF.PubTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private const string _ip = "192.168.28.132";
        private const string _user = "xxf";
        private const string _pwd = "123456";
        private const string _workPath = "/home/xxf";
        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("点击事件");
            var connectionInfo = new ConnectionInfo(_ip,
                                        _user,
                                        new PasswordAuthenticationMethod(_user, _pwd)
                                        //new PrivateKeyAuthenticationMethod("rsa.key"))
                                        );
            using (var client = new SftpClient(connectionInfo))
            {
                client.Connect();
                Console.WriteLine($"{client.ConnectionInfo.ServerVersion},{client.OperationTimeout},{client.IsConnected}");
                client.ChangeDirectory(_workPath);
                bool listDirectoryDelegateCalled = false;
                IAsyncResult asyncResult = null;
                asyncResult = client.BeginListDirectory(client.WorkingDirectory, delegate (IAsyncResult ar)
                {
                    var dirs = client.EndListDirectory(ar);
                    Console.WriteLine($"数量{dirs.Count()}");
                    foreach (var item in dirs.OrderByDescending(m => m.LastWriteTime))
                    {
                        Console.WriteLine($"name:{item.FullName}|LastWriteTime:{item.LastWriteTime}|LastAccessTime:{item.LastAccessTime}");
                    }
                    listDirectoryDelegateCalled = true;
                }, null);

                //while (!asyncResult.IsCompleted)
                //{
                //    Console.WriteLine("等待5秒");
                //    System.Threading.Thread.Sleep(5000);
                //}
                Console.WriteLine(listDirectoryDelegateCalled);
            }

            //using (var client = new SshClient(connectionInfo))
            //{

            //    client.Connect();
            //    Console.WriteLine($"{client.ConnectionInfo.ServerVersion},{client.KeepAliveInterval.TotalSeconds},{client.IsConnected}");
            //    //var term= client.CreateCommand("export TERM=dumb");
            //    var term = client.CreateCommand("echo $TERM");
            //    term.Execute();
            //    Console.Write(term.Result);
            //    var command= client.CreateCommand("top",Encoding.UTF8);

            //    //var r= command.Execute();
            //    //testR = back;
            //    //Action<string> act = new Action<string>();
            //    var r= command.BeginExecute();
            //    DateTime begin = DateTime.Now;
            //    while (!r.IsCompleted&&(DateTime.Now-begin)<TimeSpan.FromSeconds(10))
            //    {
            //        Console.WriteLine(r.AsyncState?.GetType());
            //    }
            //    command.EndExecute(r);

            //    Console.WriteLine(command.Error);
            //    Console.WriteLine(command.Result);
            //    Console.WriteLine(r);
            //    command.CancelAsync();
            //}

        }
    }
}
